﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Campaigns;
using iOak.Business.Resumes;
using iOak.Domain.Resumes;
using iOak.Business.Users;
using iOak.Business.Campaigns;

namespace iOak.Web.Administration
{
    public partial class PolicyList : BasePage
    {
        #region 页面事件
        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            pagerControl.PageIndexChange += new Modules.PagerControl.OnPageIndexChangeHandler(pagerControl_PageIndexChange);
            if (!Page.IsPostBack)
            {
                BindPolicyList();
            }
        }

        protected void pagerControl_PageIndexChange(object serder, EventArgs e)
        {
            BindPolicyList();
        }

        /// <summary>
        /// gvPolicy行绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvPolicy_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Policy policy = e.Row.DataItem as Policy;
                Label lblResumeTemplateName = e.Row.FindControl("lblResumeTemplateName") as Label;
                ResumeTemplateService resumeTemplateService = new ResumeTemplateService();
                ResumeTemplate resumeTemplate = resumeTemplateService.Retrieve(policy.ResumeTemplateId);
                if (resumeTemplate != null)
                {
                    lblResumeTemplateName.Text = resumeTemplate.Title;
                }
                Label lblPublishBy = e.Row.FindControl("lblPublishBy") as Label;
                lblPublishBy.Text = policy.PublishedBy;
            }
        }

        /// <summary>
        /// 检索按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFind_Click(object sender, EventArgs e)
        {
            gvPolicy.PageIndex = 0;
            BindPolicyList();
        }

        protected void btnPostBack_Click(object sender, EventArgs e)
        {
            Guid id;
            switch (hidEventName.Value)
            {
                case "delete":
                    id = Guid.Parse(hidEventArguments.Value);
                    DeletePolicy(id);
                    break;
            }
        }
        #endregion

        #region 处理函数
        /// <summary>
        /// 绑定Policy列表
        /// </summary>
        private void BindPolicyList()
        {
            PolicyService policyService = new PolicyService();
            //检索符合条件的policy
            List<Policy> policyList = policyService.FindPolicies(txtSubject.Text.Trim(), txtDescription.Text.Trim());
            pagerControl.BindData(gvPolicy, policyList);
        }

        private void DeletePolicy(Guid id)
        {
            PolicyService policyService = new PolicyService();
            policyService.Delete(id);
            BindPolicyList();
        }

        #endregion
    }
}